﻿// description  : 配置解析
// author       : tristan
// created      : 2011/2/16
// contact      : snaprack@gmail.com


namespace RBAC.Config
{
    using System;
    using System.Linq;
    using System.Configuration;
    using System.Collections.Generic;

    /// <summary>
    /// 访问控制配置服务
    /// </summary>
    public static class AccessControlConfigure
    {
        const string SECTION = "rbac";
        const string ON = "on";

        static readonly AccessControlSection _section = GetSection();

        /// <summary>
        /// 客户系统的名称
        /// </summary>
        public static string SystemName
        {
            get {
                return _section.System;
            }
        }
        /// <summary>
        /// 获得数据库连接字符串
        /// </summary>
        public static string ConnectionName
        {
            get
            {
                return _section.Connection;
            }
        }
        /// <summary>
        /// 是否已开启访问控制
        /// </summary>
        public static bool IsTurnedOn
        {
            get
            {
                return _section.Turn.Equals(ON, StringComparison.CurrentCultureIgnoreCase);
            }
        }
        /// <summary>
        /// 获得忽略清单
        /// </summary>
        public static IList<string> IgnoreList
        {
            get
            {
                IList<string> list = new List<string>();
                foreach (var item in _section.Ignores)
                {
                    list.Add((item as IgnoreItem).Path);
                }
                return list;
            }
        }

        /// <summary>
        /// 获得配置节点
        /// </summary>
        private static AccessControlSection GetSection()
        {
            try
            {
                return ConfigurationManager.GetSection(SECTION) as AccessControlSection;
            }
            catch
            {
                throw new AccessControlException("无法解析配置项。");
            }
        }
    }

    //-------------------------------------------------------------------------------------------------------
    //-------------------------------------------------------------------------------------------------------
    //-------------------------------------------------------------------------------------------------------

    /// <summary>
    /// 权限管理自定义配置块解析类
    /// </summary>
    public class AccessControlSection : ConfigurationSection
    {
        /// <summary>
        /// 客户系统的名称
        /// </summary>
        [ConfigurationProperty("system", IsRequired = false)]
        public string System
        {
            get { return this["system"].ToString(); }
            set { this["system"] = value; }
        }

        /// <summary>
        /// 是否开启访问控制
        /// </summary>
        [ConfigurationProperty("turn", IsRequired = true)]
        public string Turn
        {
            get { return this["turn"].ToString(); }
            set { this["turn"] = value; }
        }

        /// <summary>
        /// 数据库连接字符串的名称, 该连接串须在connectionString节点中有配置。
        /// </summary>
        [ConfigurationProperty("connection", IsRequired = true)]
        public string Connection
        {
            get { return this["connection"].ToString(); }
            set { this["connection"] = value; }
        }

        /// <summary>
        /// 忽略清单, 在此清单中出现的页面将不被拦截。
        /// </summary>
        [ConfigurationProperty("ignores", IsDefaultCollection=false)]
        [ConfigurationCollection(typeof(IgnoreItem),
            AddItemName = "add",
            ClearItemsName = "clear",
            RemoveItemName = "remove")]
        public IgnoreCollection Ignores
        {
            get { return (IgnoreCollection)this["ignores"]; }
            set { this["ignores"] = value; }
        }

    }
    /// <summary>
    /// 忽略清单
    /// </summary>
    public class IgnoreCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new IgnoreItem();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((IgnoreItem)element).Path;
        }
    }
    /// <summary>
    /// 忽略项
    /// </summary>
    public class IgnoreItem : ConfigurationElement
    {
        [ConfigurationProperty("path", IsRequired = true, IsKey = true)]
        public string Path
        {
            get { return this["path"].ToString(); }
            set { this["path"] = value; }
        }
    }

}
